package com.wx.task.future.fj;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

/**
 * 斐波那契测试
 * 利用ForkJoin线程池
 *
 * @author wang.xun
 * @date 2023/08/04
 */
@Slf4j
public class FibonacciTest {
    public static void main(String[] args) {
        ForkJoinPool forkJoinPool = new ForkJoinPool(4);
        Integer rs = forkJoinPool.invoke(new Fibonacci(8));
        log.info("结果是:{}",rs);
    }

    static class Fibonacci extends RecursiveTask<Integer>{

        private int value;

        public Fibonacci(int value) {
            this.value = value;
        }

        @Override
        protected Integer compute() {
            if(value <= 1){
                return value;
            }
            log.info("进入计算任务 value:{}",value);
            Fibonacci f1 = new Fibonacci(value - 1);
            f1.fork();
            Fibonacci f2 = new Fibonacci(value - 2);
            Integer compute = f2.compute();
            return compute + f1.join();

        }
    }
}
